/* 
 * File:   GameWindow.cpp
 * Author: jose
 * 
 * Created on March 30, 2012, 6:14 PM
 */

#include "GameWindow.h"
#include "SdlUtils.h"

extern const int SCREEN_HEIGHT;
extern const int SCREEN_WIDTH;
extern const int SCREEN_BPP;
SDL_Rect clips[ 2 ];
const int PLAYER1 = 0;
const int PLAYER2 = 1;

GameWindow::GameWindow() {
    background = SdlUtils::loadImageColorKey("tabuleiro.png");
    playersPictures = SdlUtils::loadImageColorKey("players.png");

    playersturn = 1;
    position = -1;

    //Clip the sprite sheet
    clips[ PLAYER1 ].x = 0;
    clips[ PLAYER1 ].y = 0;
    clips[ PLAYER1 ].w = 200;
    clips[ PLAYER1 ].h = 200;

    clips[ PLAYER2 ].x = 200;
    clips[ PLAYER2 ].y = 0;
    clips[ PLAYER2 ].w = 200;
    clips[ PLAYER2 ].h = 200;


}

GameWindow::GameWindow(const GameWindow& orig) {
}

GameWindow::~GameWindow() {
    SDL_FreeSurface(background);
}

void GameWindow::handleEvents(SDL_Event *event) {
    int x = 0, y = 0;
    position = -1;
    //If a mouse button was pressed
    if (event->type == SDL_MOUSEBUTTONDOWN) {
        //If the left mouse button was pressed
        if (event->button.button == SDL_BUTTON_LEFT) {
            //Get the mouse offsets

            x = event->motion.x;
            y = event->motion.y;

            int linha = -1;
            int coluna = -1;
            if (y < SCREEN_HEIGHT / 3)
                linha = 0;
            else if (y < SCREEN_HEIGHT * 2 / 3)
                linha = 1;
            else
                linha = 2;
            if (x < SCREEN_WIDTH / 3)
                coluna = 0;
            else if (x < SCREEN_WIDTH * 2 / 3)
                coluna = 1;
            else
                coluna = 2;

            position = linha * 3 + coluna;
        }
    }

}

void GameWindow::processLogic(GameStatus *gameStatus) {
    
    if(gameStatus->isGameFinished())
    {
        gameStatus->setNextState(GameStatus::STATE_END_GAME);
        return;
    }
    
    GameStatus::player currplayer = GameStatus::PLAYER_NONE;
    if (position != -1) {
        if(!gameStatus->isPositionEmpty(position))
        {
            position = -1;
            return;
        }
        if (playersturn == 1) {
            currplayer = GameStatus::PLAYER_ONE;
            //if playing alone next turn is computer, else is player 2
            if (gameStatus->numberOfPlayers == 1)
                playersturn = 0;
            else
                playersturn = 2;
        } else if (playersturn == 2) {
            currplayer = GameStatus::PLAYER_TWO;
            playersturn = 1;
        } else if (playersturn == 0) {
            currplayer = GameStatus::PLAYER_TWO;
            //PLAYER VS COM
            //add IA HERE       

            playersturn = 1;
        }
        gameStatus->setPlayerAtPosition(currplayer, position);
        position = -1;
        gameBoard = gameStatus->getGameBoard();
    }
}

void GameWindow::render(SDL_Surface *screen) {

    SdlUtils::applySurface(0, 0, background, screen);

    int pos = 0;
    int linha;
    int coluna;

    for (std::vector<GameStatus::player>::iterator Iter = gameBoard.begin(); Iter < gameBoard.end(); Iter++) {
        if (*Iter != 0) {
            linha = pos / 3;
            coluna = pos % 3;
            SdlUtils::applySurface(coluna * SCREEN_WIDTH / 3, linha * SCREEN_HEIGHT / 3, playersPictures, screen, &clips[*Iter - 1]);
        }
        pos++;
    }



}

/*lineRGBA(screen, 
           20, 10,
           70, 90, 
           255, 0, 0, 255);*/
